#define _CRT_SECURE_NO_WARNINGS 1

class Solution {
public:
    vector<vector<int>> levelOrder(TreeNode* root) {
        queue<TreeNode*> q;
        vector<vector<int>> vv;

        if (root)
            q.push(root);
        size_t qsize = 1;
        while (!q.empty())
        {
            vector<int> v;
            while (qsize--)
            {
                TreeNode* top = q.front();
                q.pop();

                v.push_back(top->val);
                if (top->left)
                    q.push(top->left);
                if (top->right)
                    q.push(top->right);
            }
            vv.push_back(v);
            qsize = q.size();
        }
        return vv;
    }
};